/* $Id: selectzone.js 2383 2006-11-05 10:43:02Z scottye $ */


/**
 * SelectZone 类
 */
function SelectZone()
{
		this.filters   = new Object();  // 过滤条件
		this.id				 = arguments[0] ? arguments[0] : 1;  			// 1 商品关联 2 组合、赠品（带价格）
		this.sourceSel = arguments[1] ? arguments[1] : null;    // 源   select 对象
		this.targetSel = arguments[2] ? arguments[2] : null;    // 目标 select 对象
		this.priceObj  = arguments[3] ? arguments[3] : null;    // 价格输入框对象
		this.filename  = location.href.substring((location.href.lastIndexOf("/")) + 1, location.href.lastIndexOf("?")) + "?act=ajax";

		/**
		 * 显示/隐藏"正在载入"
		 * @param	bool	disp 显示/隐藏
		 */
		this.toggleLoader = function(disp)
		{
		    document.getElementsByTagName('body').item(0).style.cursor = (disp) ? "wait" : 'auto';
		    
		    var doc = top.frames['header-frame'].document;
		    var loader = doc.getElementById("load-div");
		
		    loader.style.display = disp ? "block" : "none";
		}
		
		/**
		 * 载入源select对象的options
		 * @param	string		funcName	ajax函数名称
		 * @param	function	response	处理函数
		 */
		this.loadOptions = function(funcName, response)
		{
		    this.toggleLoader(true);
		    cp.set_response_type("object");
		    cp.call(this.filename, funcName, response, this.filters);
		}
		
		/**
		 * 将返回的数据解析为options的形式
		 * @param	result   	返回的数据
		 */
		this.loadOptionsResponse = function(result)
		{
		    this.toggleLoader(false);
		
				/* 清除 options */
				for (i = this.sourceSel.options.length - 1; i >=0; i--)
				{
				    this.sourceSel.options[i] = null;
				}
				
		    /* 创建 options */
		    var options = result.ajaxResponse[0].select[0].options[0];
		    if (options.option)
		    {
		        for (i = 0; i < options.option.length; i++)
		        {
		            var opt = document.createElement("OPTION");
		            opt.value = options.option[i].get_attribute("value");
		            opt.text  = options.option[i].get_attribute("text");
		            if (options.option[i].get_attribute("data"))
		            {
		                opt.id = options.option[i].get_attribute("data");
		            }
		            this.sourceSel.options.add(opt);
		        }
		    }
		    
		    /* 记录过滤条件 */
		    var filters = result.ajaxResponse[0].select[0].filters[0];
		    this.filters = new Object();
		    
		    if (filters.filter)
		    {
		        for (i = 0; i < filters.filter.length; i++)
		        {
		            this.filters[filters.filter[i].get_attribute("name")] = filters.filter[i].get_attribute("value");
		        }
		    }
		    else
		    {
		        this.filters.length = 0;
		    }
		}
		
		/**
		 * 检查对象
		 * @return boolean
		 */
		this.check = function()
		{
		    /* source select */
		    if (!this.sourceSel)
		    {
		        alert('source select undefined');
		        return false;
		    }
		    else
		    {
		        if (this.sourceSel.nodeName != 'SELECT')
		        {
		            alert('source select is not SELECT');
		            return false;
		        }
		    }
		
		    /* target select */
		    if (!this.targetSel)
		    {
		        alert('target select undefined');
		        return false;
		    }
		    else
		    {
		        if (this.targetSel.nodeName != 'SELECT')
		        {
		            alert('target select is not SELECT');
		            return false;
		        }
		    }
		    
		    /* price object */
		    if (this.id == 2 && !this.priceObj)
		    {
		        alert('price obj undefined');
		        return false;
		    }
		    
		    return true;
		}
		
		/**
		 * 添加选中项
		 * @param	string		funcName	ajax函数名称
		 * @param	function	response	处理函数
		 * @param	mix				arguments 其他参数，下标从[2]开始
		 */
		this.addItem = function(funcName, response)
		{
		    if (!this.check())
		    {
		        return;
		    }
		    
		    var source = this.sourceSel;
		    var target = this.targetSel;
			
		    for(var i = 0; i < source.length; i++)
		    {
		        var s_opt = source.options[i];
		        if (s_opt.selected)
		        {
		            for (var j = 0; j < target.length; j++)
		            {
		                var t_opt = target.options[j];
		                if (t_opt.value == s_opt.value)
		                {
		                    break;
		                }
		            }
		            if(j >= target.length)
		            {
                    cp.call(this.filename, funcName, response, s_opt.value, arguments[2], arguments[3], arguments[4]);
		            }
		        }
		    }
		}
		
		/**
		 * 添加所有项
		 * @param	string		funcName	ajax函数名称
		 * @param	function	response	处理函数
		 * @param	mix				arguments 其他参数，下标从[2]开始
		 */
		this.addAllItem = function(funcName, response)
		{
		    if (!this.check())
		    {
		        return;
		    }
		
		    var source = this.sourceSel;
		    var target = this.targetSel;
			
		    for(var i = 0; i < source.length; i++)
		    {
		        var s_opt = source.options[i];
		        for (var j = 0; j < target.length; j++)
		        {
		            var t_opt = target.options[j];
		            if (t_opt.value == s_opt.value)
		            {
		                break;
		            }
		        }
		        if(j >= target.length)
		        {
                cp.call(this.filename, funcName, response, s_opt.value, arguments[2], arguments[3], arguments[4]);
		        }
		    }
		}
		
		/**
		 * 处理添加项返回的函数
		 */
		this.addItemResponse = function(result)
		{
		    success     = eval(result.ajaxResponse[0].success[0].data.toLowerCase());
		    message     = result.ajaxResponse[0].message[0].data;
		    newvalue    = result.ajaxResponse[0].newvalue[0].data;
		
		    if (success)
		    {
		        for (var i = 0; i < this.sourceSel.options.length; i++)
		        {
		            if (this.sourceSel.options[i].value == newvalue)
		            {
		                var s_opt = this.sourceSel.options[i];
		                var opt = document.createElement('OPTION');
		                // 根据 id 取 text
		                opt.text = (this.id == 1) ? ((message.length > 0) ? s_opt.text + ' -- [' + message + ']' : s_opt.text) : s_opt.text + ' ---- [' + this.priceObj.value + ']';
		                opt.value = s_opt.value;
		                this.targetSel.options.add(opt);
		                
		                break;
		            }
		        }
		    }
		    else
		    {
    		    if (message.length > 0)
		        {
		            alert(message);
		        }
		    }
		}
		
		/**
		 * 删除选中项
		 * @param	string		funcName	ajax函数名称
		 * @param	function	response	处理函数
		 */
		this.dropItem = function(funcName, response)
		{
		    if (!this.check())
		    {
		        return;
		    }
		
		    var target = this.targetSel;
			
		    for(var i = target.length - 1; i >= 0 ; i--)
		    {
		        if (target.options[i].selected)
		        {
		            /* 调用删除项函数 */
		            cp.call(this.filename, funcName, response, target.options[i].value, arguments[2], arguments[3], arguments[4]);
		        }
		    }
		}
		
		/**
		 * 删除所有项
		 * @param	string		funcName	ajax函数名称
		 * @param	function	response	处理函数
		 */
		this.dropAllItem = function(funcName, response)
		{
		    if (!this.check())
		    {
		        return;
		    }
		
		    var target = this.targetSel;
			
		    for(var i = target.length - 1; i >= 0 ; i--)
		    {
		        /* 调用删除项函数 */
		        cp.call(this.filename, funcName, response, target.options[i].value, arguments[2], arguments[3], arguments[4]);
		    }
		}
		
		/**
		 * 处理删除项返回的函数
		 */
		this.dropItemResponse = function(result)
		{
		    success     = eval(result.ajaxResponse[0].success[0].data.toLowerCase());
		    message     = result.ajaxResponse[0].message[0].data;
		    newvalue    = result.ajaxResponse[0].newvalue[0].data;

		    if (success)
		    {
		        for (var i = 0; i < this.targetSel.options.length; i++)
		        {
		            if (this.targetSel.options[i].value == newvalue)
		            {
		                this.targetSel.options[i] = null;
		                break;
		            }
		        }
		    }
		
		    if (message.length > 0)
		    {
		        alert(message);
		    }
		}
}